package com.yanqu.road.server.cmd.strongholdwar;

import com.yanqu.road.logic.cross.stronghold.CrossActivityUnionRankModel;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.Collection;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_SILVER_UNION_RANK,desc = "跨服银两捐献排行")
public class CrossStongholdSilverRankCmd extends CrossUnionCommand {
    private int activityId;
    private String unionId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.CrossStrongholdSilverDonateRankReq req = StrongholdWarProto.CrossStrongholdSilverDonateRankReq.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        unionId = req.getUnionId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        if (CrossStrongholdActivityMgr.isAfterWildTime(activityId)) {
            int conditionType = CrossStrongholdActivityMgr.CONDITION_TYPE_SILVER_DONAGE;
            UnionActivityRankProto.UnionActivityRankListMsg tempList = CrossStrongholdRankMgr.getCrossActivityUnionRankListModel(activityId, groupId, conditionType, true).getRankListMsg().build();
            Collection<String> qualifyUnionList = CrossStrongholdMgr.getQualifyUnionList(activityId, groupId);
            BigInteger myUnionValue = BigInteger.ZERO;
            UnionActivityRankProto.UnionActivityRankListMsg.Builder rankListMsg = UnionActivityRankProto.UnionActivityRankListMsg.newBuilder();
            for (UnionActivityRankProto.UnionActivityRankTempMsg tempMsg : tempList.getRankListList()) {
                if (qualifyUnionList.contains(tempMsg.getUnionUid())) {
                    rankListMsg.addRankList(tempMsg);
                }
                if (tempMsg.getUnionUid().equals(unionId)) {
                    myUnionValue = new BigInteger(tempMsg.getValue());
                }
            }

            StrongholdWarProto.StrongholdSilverDonateDataRespMsg.Builder resp = StrongholdWarProto.StrongholdSilverDonateDataRespMsg.newBuilder();
            StrongholdWarProto.StrongholdSilverDonateDataMsg.Builder msg = StrongholdWarProto.StrongholdSilverDonateDataMsg.newBuilder();
            msg.setRank(CrossStrongholdMgr.getSilverRank(activityId, unionId));
            msg.setTotalDonate(myUnionValue.toString());
            resp.setData(msg);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(ClientProtocol.U_STRONGHOLD_WAR_SILVER_DONATE_DATA, resp);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
        }else {
            int rank = CrossStrongholdRankMgr.getCrossActivityUnionRank(activityId, CrossStrongholdActivityMgr.CONDITION_TYPE_SILVER_DONAGE, serverId, unionId);
            CrossActivityUnionRankModel model = CrossStrongholdRankMgr.getCrossActivityUnionRankListModel(activityId, groupId, CrossStrongholdActivityMgr.CONDITION_TYPE_SILVER_DONAGE, true);
            BigInteger unionValue = model.getMyUnionValue(serverId, unionId);
            StrongholdWarProto.StrongholdSilverDonateDataRespMsg.Builder resp = StrongholdWarProto.StrongholdSilverDonateDataRespMsg.newBuilder();
            StrongholdWarProto.StrongholdSilverDonateDataMsg.Builder msg = StrongholdWarProto.StrongholdSilverDonateDataMsg.newBuilder();
            msg.setRank(rank);
            msg.setTotalDonate(unionValue.toString());
            resp.setData(msg);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(ClientProtocol.U_STRONGHOLD_WAR_SILVER_DONATE_DATA, resp);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
        }


    }

    @Override
    public void sendData() {

    }
}
